<?php
// +----------------------------------------------------------------------
// | 黄骅麻将 [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
/**
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');


class AgentsController extends AdminbaseController{
	
	protected $users_model,$role_model;
	
    public function _initialize() {
        parent::_initialize();
        $this->role_model = D("Common/Role");
		$this->users_model = D("Common/Users");
    }
    
    //代理管理列表
    public function agent(){
    	$where=array();
    	$request=I('request.');
    	 
    	if(!empty($request['agentid'])){
    		$where['AGENTID']=$request['agentid'];
    		$this->assign("agentid",$request['agentid']);
    	}
    	if($request['a_state'] !=0){
    		if($request['a_state'] == 2){
    			$where['A_STATE']=0;
    		} else {
    			$where['A_STATE']=$request['a_state'];
    		}
    		$this->assign("a_state",$request['a_state']);
    	}
    	$start_time=I('request.start_time');
    	if(!empty($start_time)){
    		$where['A_ADDTIME'][0]=array(
    				array('EGT',$start_time)
    		);
    	}
    	$end_time=I('request.end_time');
    	if(!empty($end_time)){
    		$where['A_ADDTIME'][1]=array(
    				array('ELT',$end_time)
    		);
    	}
    	$parentid=I('request.parentid');
    	if(!empty($parentid)){
    		$where['parentid']=$parentid;
    	}else{
    		$where['parentid']=array("neq","0");
    	}
    	$count = M("Jinshun_agent",null)
    	->where($where)->count();
    	$size = $page = $this->page($count, 25);
    	$data = M("Jinshun_agent",null)
    	->where($where)
    	->order("agentid DESC")
    	->limit($page->firstRow, $page->listRows)
    	->select();
    	if(!empty($data) && is_array($data)){
    		$existdata=true;
    		foreach ($data as $key=>$item){
    			if($item['parentid']){
    				$data[$key]['parentid'] = M("Jinshun_agent",null)->where(array("AGENTID"=>$item['parentid']))->getField("a_name");
    			}
    			$data[$key]['a_worktime'] = date("Y-m-d H:i:s",strtotime($item['a_worktime']));
    			$data[$key]['a_addtime'] = date("Y-m-d H:i:s",strtotime($item['a_addtime']));
    		}
    	}else{
    		$existdata=false;
    	}
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("agent", $data);
    	$this->assign("parentid", $parentid);
    	$this->assign("existdata", $existdata);
    	$this->display();
    }
    
    public function agentadd() {
    	$parentid=I('request.parentid');
    	$roles=$this->role_model->where(array('status' => 1))->order("id DESC")->select();
    	array_pop($roles);
    	$this->assign("roles",$roles);
    	$this->assign("parentid", $parentid);
    	$this->display();
    }
    public function agentadd_post() {
    	if (IS_POST) {
    		unset($_POST['sheng']);
    		unset($_POST['shi']);
    		unset($_POST['qu']);
    		$parentid=I('request.parentid');
    		$data = $_POST;
    		$count = M("Jinshun_agent",null)->where(array("A_NAME"=>$data['A_NAME']))->count();
    		if(empty($count)){
	    		$item = M("Jinshun_agent",null);
	    		$data['A_RANK'] = session("name");
	    		$data['A_CARD'] = $data['A_NAME'];
	    		$data['A_ADDTIME'] = date("Y-m-d H:i:s");
	    		$data['A_WORKTIME'] = date("Y-m-d H:i:s");
	    		$data['A_TEL'] = '0';
	    		if ($item->add($data)!==false) {
	    			$this->success("添加成功",U("Agents/agent",array("parentid"=>$parentid)));
	    		} else {
	    			$this->error("添加失败！");
	    		}
    		}else{
    			$this->error("您输入的代理账号已存在！");
    		}
    	}
    }
    public function agentedit(){
    	$parentid=I('request.parentid');
    	$roles=$this->role_model->where(array('status' => 1))->order("id DESC")->select();
    	array_pop($roles);
    	$agentid = I('get.id',0,'intval');
    	$item = M("Jinshun_agent",null)
		->where("AGENTID='$agentid'")
		->find();
    	if(!empty($item) && is_array($item)){
    		if(!empty($item['agent_area'])){
    			$arrarea = explode("_", $item['agent_area']);
    			if(!empty($arrarea) && is_array($arrarea)){
    				foreach ($arrarea as $key=>$val){
    					$arr = explode(":", $val);
    					$item['area'][$arr['0']]['level'] = $key+1;
    					$item['area'][$arr['0']]['value'] = $arr['1'];
    				}
    			}
    		}
    	}
    	$item['a_sex'] = trim($item['a_sex'],' ');
    	$this->assign("agent", $item);
    	$this->assign("roles",$roles);
    	$this->assign("parentid", $parentid);
    	$this->display();
    }
    public function agentedit_post(){
    	$parentid=I('request.parentid');
    	$agentid = I('post.id',0,'intval');
    	unset($_POST['sheng']);
    	unset($_POST['shi']);
    	unset($_POST['qu']);
    	$data = $_POST;
    	$count = M("Jinshun_agent",null)->where(array("A_NAME"=>$data['A_NAME'],"AGENTID"=>array("neq",$agentid)))->count();
    	if(empty($count)){
	    	$data['A_RANK'] = session("name");
	    	$data['A_CARD'] = $data['A_NAME'];
	    	$item = M("Jinshun_agent",null)->where(array("AGENTID"=>$agentid));
	    	if ($item->save($data)!==false) {
	    		$this->success("编辑成功",U("Agents/agent",array("parentid"=>$parentid)));
	    	} else {
	    		$this->error("编辑失败！");
	    	}
    	}else{
    		$this->error("您输入的代理账号已存在！");
    	}
    }
    public function agentenable(){//启用
    	if(isset($_POST['ids'])){
    		$ids = join( ',', $_POST[ 'ids' ] );
    		$data=array("A_STATE"=>1);
    		if (M("Jinshun_agent",null)->where(" AGENTID in ($ids)")->save($data)) {
    			$this->success("启用成功！");
    		} else {
    			$this->error("启用失败！");
    		}
    	}
    }
    public function agentdisable(){//禁用
    	if(isset($_POST['ids'])){
    		$ids = join( ',', $_POST[ 'ids' ] );
    		$data=array("A_STATE"=>0);
    		if (M("Jinshun_agent",null)->where(" AGENTID in ($ids)")->save($data)) {
    			$this->success("禁用成功！");
    		} else {
    			$this->error("禁用失败！");
    		}
    	}
    }
    public function chargecard(){//购卡记录
    	$request=I('request.');
    	if(!empty($request['agentid'])){
    		$where['G_AGENTID']=$request['agentid'];
    		$this->assign("agentid",$request['agentid']);
    	}
    	if(!empty($request['chief'])){
    		$where['G_CHIEF']=$request['chief'];
    		$this->assign("chief",$request['chief']);
    	}
    	$start_time=I('request.start_time');
    	 if(!empty($start_time)){
    	$where['G_TIME'][0]=array(
    			array('EGT',$start_time)
    	);
    	}
    	$end_time=I('request.end_time');
    	if(!empty($end_time)){
    	$where['G_TIME'][1]=array(
    			array('ELT',$end_time)
    	);
    	}
    	$count = M("Jinshun_glodwholesale",null)
    	->where($where)->count();
    	$size = $page = $this->page($count, 25);
    	$result = M("Jinshun_glodwholesale",null)
    	->where($where)
    	->order("glodwholesaleid DESC")
    	->limit($page->firstRow, $page->listRows)
    	->select();
    	if(!empty($result) && is_array($result)){
    		$existdata=true;
    		foreach ($result as $key=>$item){
    			$result[$key]['g_glodnumber'] = intval($item['g_glodnumber']);
    			$time = explode(".", $item['g_time']);
    			$result[$key]['g_time'] = $time['0'];
    		}
    	}else{
    		$existdata=false;
    	}
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("charge", $result);
    	$this->assign("existdata", $existdata);
    	$this->display();
    }
    public function chargecardadd() {//添加购卡
    	$parentid=I('request.parentid');
    	$agentid = I('get.id',0,'intval');
    	$this->assign("agentid", $agentid);
    	$this->assign("parentid", $parentid);
    	$this->display();
    }
	
    public function chargecardadd_post() {//添加购卡提交表单
    	$agentid = I('post.id',0,'intval');
    	$parentid=I('request.parentid');unset($_POST['parentid']);
    	$id = session('ADMIN_ID');
    	$user = session('name');
    	$result2=array();
    	$number = I("post.G_GLODNUMBER");
    	if(empty($number)){
    		$this->error("购卡数量必须大于0");
    	}elseif (!is_numeric($number)){
    		$this->error("购卡数量必须为数字");
    	}
    	$comment = $_POST['G_COMMENT1'].":".$_POST['G_COMMENT2'];
    	unset($_POST['id']);
    	unset($_POST['G_COMMENT1']);
    	unset($_POST['G_COMMENT2']);
    	$result1 = $_POST;
    	$agent = M("Jinshun_agent",null);
    	$item = $agent->field('a_name,a_tel')->where(array("AGENTID"=>$agentid))->find();
    	if(!empty($id) && !empty($user)){
/*     		$usertel = $this->users_model->field("A_TEL")->where(array("id"=>$id,"user_login"=>$user))->find();
    		if($usertel['a_tel'] >= $number){ */
    			//$result3['a_tel'] = $usertel['a_tel'] - $number;
    			$result1['G_COMMENT'] = $comment;
    			$result1['G_AGENTID'] = $item['a_name'];
    			$result1['G_IP'] =	get_real_ip();
    			$result2['A_WORKTIME'] = $result1['G_TIME'] = date("Y-m-d H:i:s");
    			$result1['G_CHIEF'] = session("name");
    			$result2['A_TEL'] = $number+$item['a_tel'];
    			$sale = M("Jinshun_glodwholesale",null);
    			if ($sale->add($result1)!==false) {
    				$agent->where(array("AGENTID"=>$agentid))->save($result2);
    				//$this->users_model->where(array("id"=>$id,"user_login"=>$user))->save($result3);
    				$this->success("添加成功",U("Agents/agent",array("parentid"=>$parentid)));
    			} else {
    				$this->error("添加失败！");
    			}
/*     		}else{
    			$this->error("管理员房卡数量不足！");
    		} */
    	}else{
    		$this->error('管理员数据获取失败，请完善管理员信息！', U("user/pcenteredit"));
    	}	
    	$this->display();
    }
    
    public function sellingcard(){//售卡记录
    	$request=I('request.');
    	if(!empty($request['agentid'])){
    		$where['G_AGENTID']=$request['agentid'];
    		$this->assign("agentid",$request['agentid']);
    	}
    	if(!empty($request['username'])){
    		$where['G_USERNAME']=$request['username'];
    		$this->assign("username",$request['username']);
    	}
    	$start_time=I('request.start_time');
    	 if(!empty($start_time)){
    	$where['G_TIME'][0]=array(
    			array('EGT',$start_time)
    	);
    	}
    	$end_time=I('request.end_time');
    	if(!empty($end_time)){
    	$where['G_TIME'][1]=array(
    			array('ELT',$end_time)
    	);
    	}
    	/* $id = sp_get_current_admin_id();
    	$user = $this->users_model->field('user_login')->where(array("id"=>$id))->find();
    	if(!empty($user['user_login'])){
    		$where['G_AGENTID']=$user['user_login'];
    	} */
    	$count = M("Jinshun_glodtransaction",null)
    	->where($where)->count();
    	$size = $page = $this->page($count, 25);
    	$result = M("Jinshun_glodtransaction",null)
    	->where($where)
    	->order("glodetransactionid DESC")
    	->limit($page->firstRow, $page->listRows)
    	->select();
    	if(!empty($result) && is_array($result)){
    		$existdata=true;
    		foreach ($result as $key=>$item){
    			$result[$key]['g_glodnumber'] = intval($item['g_glodnumber']);
    			$time = explode(".", $item['g_time']);
    			$result[$key]['g_time'] = $time['0'];
    		}
    	}else{
    		$existdata=false;
    	}
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("selling", $result);
    	$this->assign("existdata", $existdata);
    	$this->display();
    }
    public function sellingcardadd() {//添加购卡
    	$memid = I('get.id',0,'intval');
    	$data = M('Accountsinfo',null,'DB_CONFIG2')
    	->field('userid,accounts')
    	->where(array("UserID"=>$memid))
    	->find();
    	$this->assign("member", $data);
    	$this->display();
    }
    public function sellingcardadd_post() {//添加购卡提交表单
    	$memberid = I('post.id',0,'intval');
    	$id = session('ADMIN_ID');
    	$user = session('name');
    	$result2=array();
    	$number = I("post.G_GLODNUMBER");
    	if(empty($number)){
    		$this->error("购卡数量必须大于0");
    	}elseif (!is_numeric($number)){
    		$this->error("购卡数量必须为数字");
    	}
    	if(!empty($id) && !empty($user)){
    		$usertel = $this->users_model->field("A_TEL")->where(array("id"=>$id,"user_login"=>$user))->find();
    		if($usertel['a_tel'] >= $number){
    			$result3['a_tel'] = $usertel['a_tel'] - $number;	
    		}else{
    			$this->error("管理员房卡数量不足！");
    		}
    	}else{
    		$this->error('管理员数据获取失败，请完善管理员信息！', U("user/pcenteredit"));
    	}
    	$member = M('Gamescoreinfo',null,'DB_CONFIG3')->field("InsureScore")->where(array("UserID"=>$memberid))->find();
    	unset($_POST['id']);
    	$result1 = $_POST;
    	$result1['G_AGENTID'] = session('name');
    	$result1['G_IP'] =	get_real_ip();
    	$result2['InsureScore'] = $member['insurescore']+$number;
    	$result1['G_USERNAME'] = $memberid;
    	$result1['G_TIME'] = date("Y-m-d H:i:s");
    	$action = M("Jinshun_glodtransaction",null);
    	if ($action->add($result1)!==false) {
    		M('Gamescoreinfo',null,'DB_CONFIG3')->where(array("UserID"=>$memberid))->save($result2);
    		$this->users_model->where(array("id"=>$id,"user_login"=>$user))->save($result3);
    		$this->success("添加成功",U("Agents/index"));
    	} else {
    		$this->error("添加失败！");
    	}
    	$this->display();
    }
    public function bucklecardadd() {//扣卡
    	$parentid=I('request.parentid');
    	$agentid = I('get.id',0,'intval');
    	$this->assign("agentid", $agentid);
    	$this->assign("parentid", $parentid);
    	$this->display();
    }
    
    public function bucklecardadd_post() {//添加购卡提交表单
    	$agentid = I('post.id',0,'intval');
    	$parentid=I('request.parentid');unset($_POST['parentid']);
    	$id = session('ADMIN_ID');
    	$user = session('name');
    	$result2=array();
    	$number = I("post.G_GLODNUMBER");
    	if(empty($number)){
    		$this->error("购卡数量必须大于0");
    	}elseif (!is_numeric($number)){
    		$this->error("购卡数量必须为数字");
    	}
    	$comment = $_POST['G_COMMENT1'].":".$_POST['G_COMMENT2'];
    	unset($_POST['id']);
    	unset($_POST['G_COMMENT1']);
    	unset($_POST['G_COMMENT2']);
    	$result1 = $_POST;
    	$agent = M("Jinshun_agent",null);
    	$item = $agent->field('a_name,a_tel')->where(array("AGENTID"=>$agentid))->find();
    	if($item['a_tel'] >= $number){    		
    		$result2['A_TEL'] = $item['a_tel'] - $number;
    	}else{
    		$this->error("您输入的卡数量大于剩余卡数量，请重新输入");
    	}
    	/* if(!empty($id) && !empty($user)){
	    	$usertel = $this->users_model->field("A_TEL")->where(array("id"=>$id,"user_login"=>$user))->find();
	    	$result3['a_tel'] = $usertel['a_tel'] + $number;
    	}else{
	    	$this->error('管理员数据获取失败，请完善管理员信息！', U("user/pcenteredit"));
	    } */
    	$result1['G_COMMENT'] = $comment;
    	$result1['G_AGENTID'] = $item['a_name'];
    	$result1['G_IP'] =	get_real_ip();
    	$result2['A_WORKTIME'] = $result1['G_TIME'] = date("Y-m-d H:i:s");
    	$result1['G_CHIEF'] = session("name");
    	$sale = M("Jinshun_glodwholesale",null);
    	if ($sale->add($result1)!==false) {
    		$agent->where(array("AGENTID"=>$agentid))->save($result2);
    		//$this->users_model->where(array("id"=>$id,"user_login"=>$user))->save($result3);
    		$this->success("添加成功",U("Agents/agent",array("parentid"=>$parentid)));
    	} else {
    		$this->error("添加失败！");
    	}
    	$this->display();
    }
    
    //根据省获取市
    public function sheng(){
    	$pid = I("get.parent_id");
    	$type = I("get.type");
    	$data=M("region")->where("parent_id='$pid' and region_type='$type'")->field("region_id,parent_id,region_name,region_type")->select();
    	//$this->ajaxReturn($data);
    	exit(json_encode($data));
    }
}//结束
?>